package com.tsunami.auth.service.auth.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsunami.auth.mapper.ClientsMapper;
import com.tsunami.auth.service.auth.IClientsService;
import com.tsunami.authority.dto.auth.ClientsSaveDTO;
import com.tsunami.authority.dto.auth.ClientsUpdateDTO;
import com.tsunami.authority.entity.auth.Clients;
import com.tsunami.base.AppUtils;
import com.tsunami.base.R;
import com.tsunami.utils.MD5;
import com.tsunami.utils.PageUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 终端信息表 服务实现类
 * </p>
 *
 * @author lx
 * @since 2021-11-29
 */
@Service
public class ClientsServiceImpl extends ServiceImpl<ClientsMapper, Clients> implements IClientsService {

    @Override
    public R selectPageList(Clients data) {
        Page<Clients> pages = baseMapper.selectPageList(PageUtils.buildPage(),data);
        return R.ok().data(pages);
    }

    @Override
    public Clients saveClients(ClientsSaveDTO data) {
        Clients clients = new Clients();
        clients.setAccessTokenValidity(data.getAccessTokenValidity());
        clients.setAdditionalInformation(data.getAdditionalInformation());
        clients.setAuthorities(data.getAuthorities());
        clients.setAuthorizedGrantTypes(data.getAuthorizedGrantTypes());
        clients.setAutoapprove(data.getAutoapprove());
        clients.setCreateTime(LocalDateTime.now());
        clients.setRefreshTokenValidity(data.getRefreshTokenValidity());
        clients.setResourceIds(data.getResourceIds());
        clients.setScope(data.getScope());
        clients.setWebServerRedirectUri(data.getWebServerRedirectUri());
        super.save(clients);
        return clients;
    }

    @Override
    public boolean removeClient(Clients clients) {
        QueryWrapper param = new QueryWrapper();
        param.eq("client_id",clients.getClientId());
        remove(param);
        return true;
    }

    @Override
    public String updateSecret(String serverCode, String appId) {
        QueryWrapper param = new QueryWrapper<>();
        param.eq("client_id",appId);
        Clients clients = getOne(param);
        String clientSecret = AppUtils.getAppSecret(appId,serverCode);
        if(clients!=null){
            clients.setClientSecret(MD5.encrypt(clientSecret));
            super.updateById(clients);
            return clientSecret;
        }
        return null;
    }

    @Override
    public boolean updateRedirectUri(String appId, String redirectUri) {
        QueryWrapper param = new QueryWrapper<>();
        param.eq("client_id",appId);
        Clients clients = getOne(param);
        if(clients!=null){
            clients.setWebServerRedirectUri(redirectUri);
            super.updateById(clients);
            return true;
        }
        return false;
    }

    @Override
    public Clients updateClient(ClientsUpdateDTO data) {
        QueryWrapper param = new QueryWrapper<>();
        param.eq("client_id",data.getClientId());
        Clients clients = getOne(param);
        if(clients!=null){
            clients.setAccessTokenValidity(data.getAccessTokenValidity());
            clients.setAdditionalInformation(data.getAdditionalInformation());
            clients.setAuthorities(data.getAuthorities());
            clients.setAuthorizedGrantTypes(data.getAuthorizedGrantTypes());
            clients.setAutoapprove(data.getAutoapprove());
            clients.setRefreshTokenValidity(data.getRefreshTokenValidity());
            clients.setResourceIds(data.getResourceIds());
            clients.setScope(data.getScope());
            clients.setWebServerRedirectUri(data.getWebServerRedirectUri());
            super.updateById(clients);
            return clients;
        }
        return null;
    }
}
